package com.sl.hx.shop.ui.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseFragment;
import com.sl.hx.shop.bean.MessageEvent;
import com.sl.hx.shop.bean.ProductList;
import com.sl.hx.shop.ui.home.activity.GoodsDetailActivity;
import com.sl.hx.shop.ui.home.adapter.GoodsListAdapter;
import com.sl.hx.shop.ui.home.contacts.GoodsListContacts;
import com.sl.hx.shop.ui.home.presenter.GoodsListPresenter;
import com.sl.hx.shop.utils.ScreenUtil;
import com.sl.hx.shop.utils.UserDataManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GoodsListFragment extends BaseFragment<GoodsListContacts.view,GoodsListPresenter> implements GoodsListContacts.view {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    private int mId;
    private int mFavoritesId;
    private GoodsListAdapter mGoodsListAdapter;
    private List<ProductList> mProductLists = new ArrayList<>();
    private List<ProductList> history = new ArrayList<>();
    private int page = 1;
    private int num = 10;
    private boolean isLoadMore = false;
    private GridLayoutManager manager;

    @Override
    protected GoodsListPresenter createPresenter() {
        return new GoodsListPresenter(mActivity);
    }

    @Override
    protected int getViewLayoutId() {
        return R.layout.fragment_goodslist;
    }

    @Override
    protected void initView(View mView) {
        EventBus.getDefault().register(this);
        if (getArguments() != null) {
            mId = getArguments().getInt("goods_id");
            mFavoritesId = getArguments().getInt("favorites_id");
        }
        initRecycle();
        mPresenter.getProductLists(mFavoritesId, page, num);
        mPresenter.indexCount(mId);
    }

    private void initRecycle() {
        mGoodsListAdapter = new GoodsListAdapter(mProductLists);
        manager = new GridLayoutManager(mActivity,2);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mGoodsListAdapter);
        mRecyclerView.addItemDecoration(new MarginDecoration(mContext));
        mGoodsListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                isLoadMore=true;
                mPresenter.getProductLists(mFavoritesId,page,num);
            }
        },mRecyclerView);
        mGoodsListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //历史记录保存数据
                if (history.size() < 30) {
                    for (int i = 0; i < history.size(); i++) {
                        if (history.get(i).getNum_iid().equals(mGoodsListAdapter.getItem(position).getNum_iid())) {
                            history.remove(history.get(i));
                        }
                    }
                    history.add(mGoodsListAdapter.getItem(position));
                    UserDataManager.saveHistory(history);
                }
                //跳转
                Intent intent = new Intent(mActivity, GoodsDetailActivity.class);
                intent.putExtra("user_type",mGoodsListAdapter.getItem(position).getUser_type());
                intent.putExtra("num_iid",mGoodsListAdapter.getItem(position).getNum_iid());
                intent.putExtra("Coupon_click_url",mGoodsListAdapter.getItem(position).getCoupon_click_url());
                intent.putExtra("coupon_info",mGoodsListAdapter.getItem(position).getCoupon_info());
                startActivity(intent);
            }
        });

    }

    @Override
    public void ShowProductList(List<ProductList> productLists) {
        if (isLoadMore){
            if (productLists != null && productLists.size() > 0) {
                mGoodsListAdapter.addData(productLists);
            }
            isLoadMore=false;
        }else {

            mGoodsListAdapter.setNewData(productLists);
        }

        if (productLists.size() < num) {
            mGoodsListAdapter.loadMoreEnd(true);
        } else {
            mGoodsListAdapter.loadMoreComplete();
        }
    }

    //recycleview item设置间距
    class MarginDecoration extends RecyclerView.ItemDecoration {
        private int margintop,marginleft,marginright;

        public MarginDecoration(Context context) {
            margintop = ScreenUtil.dp2px(context,4);
            marginleft = ScreenUtil.dp2px(context,16);
            marginright = ScreenUtil.dp2px(context,4);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (parent.getChildLayoutPosition(view) %2 ==0) {
                outRect.set(marginleft,margintop,marginright,margintop);
            }else {
                outRect.set(marginright,margintop,marginleft,margintop);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        if (messageEvent.isScrollow()){
            mRecyclerView.setNestedScrollingEnabled(true);
        }else {
            mRecyclerView.setNestedScrollingEnabled(false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
